/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dw2;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import javax.swing.*;

/**
 *
 * @author Amanda
 */
public class GameOverMenu extends Menu implements ActionListener{
    private ImageButton toMainMenu;
    private ImageButton toHighscores;
    private Image[] images;
    private boolean victory;

    public GameOverMenu(boolean victory, Main p, Image[] images)
    {
        super(p, null);
        this.victory = victory;
        this.images = images;
        toMainMenu = new ImageButton(images[0], "Main menu", 298, 156);
        toHighscores = new ImageButton(images[1], "Highscores", 298, 153);

        if(!victory) { //Game Over image only displays if game did not end in victory
            JLabel gameOver = new JLabel();
            ImageIcon gOImage = new ImageIcon(images[3]);
            gameOver.setIcon(gOImage);
            gameOver.setAlignmentX(CENTER_ALIGNMENT);

            contents.add(Box.createRigidArea(new Dimension(75, 75)));
            contents.add(gameOver);
        }

        JPanel buttonPanel = new JPanel();
        buttonPanel.setOpaque(false);
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.add(toMainMenu);
        buttonPanel.add(Box.createRigidArea(new Dimension(90, 10)));
        buttonPanel.add(toHighscores);

        contents.add(buttonPanel);
    }

    public void init() {
        toMainMenu.addActionListener(this);
        toHighscores.addActionListener(this);
    }

    public void achievedHighscoreButton(boolean b)
    {
        if(b)
        {
            ImageIcon saveScore = new ImageIcon(images[2]);
            toHighscores.setIcon(saveScore);
            toHighscores.repaint();
        }
        else
        {
            ImageIcon saveScore = new ImageIcon(images[1]);
            toHighscores.setIcon(saveScore);
            toHighscores.repaint();
        }
    }

    public void setBG(){
        if(victory) {
            //If this is a victory screen, the background should not be captured
            //Instead, use the background image
            image = images[3];
            return;
        }
        BufferedImage bimage = (BufferedImage)parent.screenCapture();

        //RescaleOp rop = new RescaleOp(-1.0f, 255f, null);
        //BufferedImage negative = rop.filter(bimage, null);

        image = bimage;

        parent.track.addImage(image, 100);
        try
        {
            parent.track.waitForID(100);
        }
        catch(InterruptedException ex)
        {
        }
    }

    @Override
    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        if(!victory) { //effects only needed for defeat screen
            Graphics2D g2 = (Graphics2D)g;
            /* Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.85F);
             g2.setComposite(c);

            Rectangle2D r1 = new Rectangle2D.Double(0, 0, 800, 600);
            Color darkRed = new Color(139,26,26);
            g2.setPaint(darkRed);
            g2.fill(r1);*/
            Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1F);
            g2.setComposite(c);
      }


    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("Main menu")) {
            parent.hscoreMenu.release();
            parent.remove(this);
            parent.add(parent.mainMenu);
            parent.validate();
            parent.repaint();

            parent.MC.playMenuMusic(0);
        }

        else if(e.getActionCommand().equals("Highscores")) {
            parent.remove(this);
            parent.add(parent.hscoreMenu);
            parent.validate();
            parent.repaint();

            parent.MC.playMenuMusic(2);
        }
    }

}
